{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "from sklearn.model_selection import KFold\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import precision_recall_curve\n",
    "from sklearn import preprocessing\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import time\n",
    "import random\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import interp\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "from sklearn.ensemble import ExtraTreesClassifier\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.linear_model import SGDClassifier\n",
    "from sklearn import svm\n",
    "from collections import Counter\n",
    "from tqdm import tqdm\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "from sklearn.ensemble import GradientBoostingClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "from sklearn.metrics import roc_auc_score, auc\n",
    "from sklearn.metrics import precision_recall_fscore_support\n",
    "from sklearn.metrics import precision_recall_curve\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.metrics import roc_curve\n",
    "from sklearn.metrics import precision_recall_curve"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data(directory):\n",
    "    GSSM = np.loadtxt(directory + '\\GSSM_.txt',dtype=np.float32)\n",
    "    PESSM = np.loadtxt(directory + '\\PSSM.txt',dtype=np.float32,delimiter='\\t')\n",
    "\n",
    "    IPE = pd.DataFrame(PESSM).reset_index()\n",
    "    IG = pd.DataFrame(GSSM).reset_index()\n",
    "    IPE.rename(columns = {'index':'id'}, inplace = True)\n",
    "    IG.rename(columns = {'index':'id'}, inplace = True)\n",
    "    IPE['id'] = IPE['id']\n",
    "    IG['id'] = IG['id']\n",
    "    \n",
    "    return IPE, IG\n",
    "\n",
    "def sample(directory, random_seed):\n",
    "    all_associations = pd.read_csv(directory + '/all_gpe_pairs.csv')\n",
    "    known_associations = all_associations.loc[all_associations['label'] == 1]\n",
    "    unknown_associations = all_associations.loc[all_associations['label'] == 0]\n",
    "    random_negative = unknown_associations.sample(n=known_associations.shape[0], random_state=random_seed, axis=0)\n",
    "\n",
    "    sample_df = known_associations.append(random_negative)\n",
    "    sample_df.reset_index(drop=True, inplace=True)\n",
    "    return sample_df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "def performances(y_true, y_pred, y_prob):\n",
    "\n",
    "    tn, fp, fn, tp = confusion_matrix(y_true, y_pred, labels = [0, 1]).ravel().tolist()\n",
    "\n",
    "    pos_acc = tp / sum(y_true)\n",
    "    neg_acc = tn / (len(y_pred) - sum(y_pred)) # [y_true=0 & y_pred=0] / y_pred=0\n",
    "    accuracy = (tp+tn)/(tn+fp+fn+tp)\n",
    "    \n",
    "    recall = tp / (tp+fn)\n",
    "    precision = tp / (tp+fp)\n",
    "    f1 = 2*precision*recall / (precision+recall)\n",
    "    \n",
    "    roc_auc = roc_auc_score(y_true, y_prob)\n",
    "    prec, reca, _ = precision_recall_curve(y_true, y_prob)\n",
    "    aupr = auc(reca, prec)\n",
    "    \n",
    "    print('tn = {}, fp = {}, fn = {}, tp = {}'.format(tn, fp, fn, tp))\n",
    "    print('y_pred: 0 = {} | 1 = {}'.format(Counter(y_pred)[0], Counter(y_pred)[1]))\n",
    "    print('y_true: 0 = {} | 1 = {}'.format(Counter(y_true)[0], Counter(y_true)[1]))\n",
    "    print('acc={:.4f}|precision={:.4f}|recall={:.4f}|f1={:.4f}|auc={:.4f}|aupr={:.4f}|pos_acc={:.4f}|neg_acc={:.4f}'.format(accuracy, precision, recall, f1, roc_auc, aupr, pos_acc, neg_acc))\n",
    "    return (y_true, y_pred, y_prob), (accuracy, precision, recall, f1, roc_auc, aupr, pos_acc, neg_acc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "def obtain_data(directory, isbalance):\n",
    "\n",
    "    IPE, IG = load_data(directory)\n",
    "\n",
    "    if isbalance:\n",
    "        dtp = sample(directory, random_seed = 1234)\n",
    "    else:\n",
    "        dtp = pd.read_csv(directory + '/all_gene_peco_pairs.csv')\n",
    "\n",
    "    gene_ids = list(set(dtp['gene_idx']))\n",
    "    peco_ids = list(set(dtp['peco_idx']))\n",
    "    random.shuffle(gene_ids)\n",
    "    random.shuffle(peco_ids)\n",
    "    print('# gene = {} | peco = {}'.format(len(gene_ids), len(peco_ids)))\n",
    "\n",
    "    gene_test_num = int(len(gene_ids) / 5)\n",
    "    peco_test_num = int(len(peco_ids) / 5)\n",
    "    print('# Test: gene = {} | peco = {}'.format(gene_test_num, peco_test_num))    \n",
    "    \n",
    "    samples = pd.merge(pd.merge(dtp, IPE, left_on = 'peco_idx', right_on = 'id'), IG, left_on = 'gene_idx', right_on = 'id')\n",
    "    samples.drop(labels = ['id_x', 'id_y'], axis = 1, inplace = True)\n",
    "    \n",
    "    return IPE, IG, dtp, gene_ids, peco_ids, gene_test_num, peco_test_num, samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_task_Tp_train_test_idx(samples):\n",
    "    kf = KFold(n_splits = 5, shuffle = True, random_state = 1234)\n",
    "\n",
    "    train_index_all, test_index_all, n = [], [], 0\n",
    "    train_id_all, test_id_all = [], []\n",
    "    fold = 0\n",
    "    for train_idx, test_idx in tqdm(kf.split(samples.iloc[:, 3:])):\n",
    "        print('-------Fold ', fold)\n",
    "        train_index_all.append(train_idx) \n",
    "        test_index_all.append(test_idx)\n",
    "\n",
    "        train_id_all.append(np.array(dtp.iloc[train_idx][['gene_idx', 'peco_idx']]))\n",
    "        test_id_all.append(np.array(dtp.iloc[test_idx][['gene_idx', 'peco_idx']]))\n",
    "\n",
    "        print('# Pairs: Train = {} | Test = {}'.format(len(train_idx), len(test_idx)))\n",
    "        fold += 1\n",
    "    return train_index_all, test_index_all, train_id_all, test_id_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_task_Tg_Tpe_train_test_idx(item, ids, dtp):\n",
    "    \n",
    "    test_num = int(len(ids) / 5)\n",
    "    \n",
    "    train_index_all, test_index_all = [], []\n",
    "    train_id_all, test_id_all = [], []\n",
    "    \n",
    "    for fold in range(5):\n",
    "        print('-------Fold ', fold)\n",
    "        if fold != 4:\n",
    "            test_ids = ids[fold * test_num : (fold + 1) * test_num]\n",
    "        else:\n",
    "            test_ids = ids[fold * test_num :]\n",
    "\n",
    "        train_ids = list(set(ids) ^ set(test_ids))\n",
    "        print('# {}: Train = {} | Test = {}'.format(item, len(train_ids), len(test_ids)))\n",
    "\n",
    "        test_idx = dtp[dtp[item].isin(test_ids)].index.tolist()\n",
    "        train_idx = dtp[dtp[item].isin(train_ids)].index.tolist()\n",
    "        random.shuffle(test_idx)\n",
    "        random.shuffle(train_idx)\n",
    "        print('# Pairs: Train = {} | Test = {}'.format(len(train_idx), len(test_idx)))\n",
    "        assert len(train_idx) + len(test_idx) == len(dtp)\n",
    "\n",
    "        train_index_all.append(train_idx) \n",
    "        test_index_all.append(test_idx)\n",
    "        \n",
    "        train_id_all.append(train_ids)\n",
    "        test_id_all.append(test_ids)\n",
    "        \n",
    "    return train_index_all, test_index_all, train_id_all, test_id_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_clf(train_index_all, test_index_all, samples, classfier):\n",
    "    \n",
    "    fold = 0\n",
    "    for train_idx, test_idx in zip(train_index_all, test_index_all):\n",
    "        print('-----------------------Fold = ', str(fold))\n",
    "\n",
    "        X = samples.iloc[:, 3:]\n",
    "        y = samples['label']\n",
    "\n",
    "        scaler = preprocessing.MinMaxScaler().fit(X.iloc[train_idx,:])\n",
    "        X = scaler.transform(X)\n",
    "\n",
    "        x_train, y_train = X[train_idx], y[train_idx]\n",
    "        x_test, y_test = X[test_idx], y[test_idx]\n",
    "\n",
    "        if classfier == 'ERT':\n",
    "            clf = ExtraTreesClassifier(random_state = 19961231)\n",
    "        elif classfier == 'GNB':\n",
    "            clf = GaussianNB()\n",
    "        elif classfier == 'DT':\n",
    "            clf = DecisionTreeClassifier(random_state = 19961231)\n",
    "        elif classfier == 'SGD':\n",
    "            clf = SGDClassifier(loss=\"modified_huber\", penalty=\"l2\", max_iter=5)\n",
    "        elif classfier == 'SVM':\n",
    "            clf = svm.SVC(C=0.1, kernel='sigmoid', degree=3, gamma='auto', probability=True)\n",
    "        elif classfier == 'LR':\n",
    "            clf = LogisticRegression(max_iter=500)\n",
    "        elif classfier == 'MLP':\n",
    "            clf = MLPClassifier() \n",
    "        elif classfier == 'GBDT':\n",
    "            clf = GradientBoostingClassifier(max_depth=5,learning_rate=0.1)   \n",
    "            \n",
    "        clf.fit(x_train, y_train)\n",
    "\n",
    "        y_train_prob = clf.predict_proba(x_train)\n",
    "        y_test_prob = clf.predict_proba(x_test)\n",
    "\n",
    "        y_train_pred = clf.predict(x_train)\n",
    "        y_test_pred = clf.predict(x_test)\n",
    "\n",
    "        print('Train:')\n",
    "        ys_train, metrics_train = performances(y_train, y_train_pred, y_train_prob[:, 1])\n",
    "        print('Test:')\n",
    "        ys_test, metrics_test = performances(y_test, y_test_pred, y_test_prob[:, 1])\n",
    "\n",
    "        fold += 1\n",
    "    \n",
    "    return ys_train, metrics_train, ys_test, metrics_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "5it [00:00, 278.39it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# gene = 11177 | peco = 24\n",
      "# Test: gene = 2235 | peco = 4\n",
      "========== isbalance = True | task = Tp\n",
      "-------Fold  0\n",
      "# Pairs: Train = 37692 | Test = 9424\n",
      "-------Fold  1\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-------Fold  2\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-------Fold  3\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-------Fold  4\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-----------------------Fold =  0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train:\n",
      "tn = 18890, fp = 0, fn = 0, tp = 18802\n",
      "y_pred: 0 = 18890 | 1 = 18802\n",
      "y_true: 0 = 18890 | 1 = 18802\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 3965, fp = 703, fn = 641, tp = 4115\n",
      "y_pred: 0 = 4606 | 1 = 4818\n",
      "y_true: 0 = 4668 | 1 = 4756\n",
      "acc=0.8574|precision=0.8541|recall=0.8652|f1=0.8596|auc=0.9414|aupr=0.9449|pos_acc=0.8652|neg_acc=0.8608\n",
      "-----------------------Fold =  1\n",
      "Train:\n",
      "tn = 18821, fp = 0, fn = 0, tp = 18872\n",
      "y_pred: 0 = 18821 | 1 = 18872\n",
      "y_true: 0 = 18821 | 1 = 18872\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 4033, fp = 704, fn = 596, tp = 4090\n",
      "y_pred: 0 = 4629 | 1 = 4794\n",
      "y_true: 0 = 4737 | 1 = 4686\n",
      "acc=0.8620|precision=0.8531|recall=0.8728|f1=0.8629|auc=0.9422|aupr=0.9436|pos_acc=0.8728|neg_acc=0.8712\n",
      "-----------------------Fold =  2\n",
      "Train:\n",
      "tn = 18881, fp = 0, fn = 0, tp = 18812\n",
      "y_pred: 0 = 18881 | 1 = 18812\n",
      "y_true: 0 = 18881 | 1 = 18812\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 3964, fp = 713, fn = 639, tp = 4107\n",
      "y_pred: 0 = 4603 | 1 = 4820\n",
      "y_true: 0 = 4677 | 1 = 4746\n",
      "acc=0.8565|precision=0.8521|recall=0.8654|f1=0.8587|auc=0.9403|aupr=0.9415|pos_acc=0.8654|neg_acc=0.8612\n",
      "-----------------------Fold =  3\n",
      "Train:\n",
      "tn = 18794, fp = 0, fn = 0, tp = 18899\n",
      "y_pred: 0 = 18794 | 1 = 18899\n",
      "y_true: 0 = 18794 | 1 = 18899\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 4034, fp = 730, fn = 631, tp = 4028\n",
      "y_pred: 0 = 4665 | 1 = 4758\n",
      "y_true: 0 = 4764 | 1 = 4659\n",
      "acc=0.8556|precision=0.8466|recall=0.8646|f1=0.8555|auc=0.9395|aupr=0.9387|pos_acc=0.8646|neg_acc=0.8647\n",
      "-----------------------Fold =  4\n",
      "Train:\n",
      "tn = 18846, fp = 0, fn = 0, tp = 18847\n",
      "y_pred: 0 = 18846 | 1 = 18847\n",
      "y_true: 0 = 18846 | 1 = 18847\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 3980, fp = 732, fn = 616, tp = 4095\n",
      "y_pred: 0 = 4596 | 1 = 4827\n",
      "y_true: 0 = 4712 | 1 = 4711\n",
      "acc=0.8569|precision=0.8484|recall=0.8692|f1=0.8587|auc=0.9410|aupr=0.9429|pos_acc=0.8692|neg_acc=0.8660\n"
     ]
    }
   ],
   "source": [
    "directory = '../../data/'\n",
    "for isbalance in [True]:\n",
    "    \n",
    "    IPE, IG, dtp, gene_ids, peco_ids, gene_test_num, peco_test_num, samples = obtain_data(directory, \n",
    "                                                                                                 isbalance)\n",
    "    for task in ['Tp','Tg','Tpe']:\n",
    "        \n",
    "        print('========== isbalance = {} | task = {}'.format(isbalance, task))\n",
    "        \n",
    "        if task == 'Tp':\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tp_train_test_idx(samples)\n",
    "            \n",
    "        elif task == 'Tg':\n",
    "            item = 'gene_idx'\n",
    "            ids = gene_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tg_Tpe_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        elif task == 'Tpe':\n",
    "            item = 'peco_idx'\n",
    "            ids = peco_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tg_Tpe_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        ys_train, metrics_train, ys_test, metrics_test = run_clf(train_index_all, test_index_all, samples, 'ERT')\n",
    "\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "5it [00:00, 277.14it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# gene = 11177 | peco = 24\n",
      "# Test: gene = 2235 | peco = 4\n",
      "========== isbalance = True | task = Tp\n",
      "-------Fold  0\n",
      "# Pairs: Train = 37692 | Test = 9424\n",
      "-------Fold  1\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-------Fold  2\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-------Fold  3\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-------Fold  4\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-----------------------Fold =  0\n",
      "Train:\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tn = 16134, fp = 2756, fn = 3363, tp = 15439\n",
      "y_pred: 0 = 19497 | 1 = 18195\n",
      "y_true: 0 = 18890 | 1 = 18802\n",
      "acc=0.8377|precision=0.8485|recall=0.8211|f1=0.8346|auc=0.9350|aupr=0.9393|pos_acc=0.8211|neg_acc=0.8275\n",
      "Test:\n",
      "tn = 3982, fp = 686, fn = 884, tp = 3872\n",
      "y_pred: 0 = 4866 | 1 = 4558\n",
      "y_true: 0 = 4668 | 1 = 4756\n",
      "acc=0.8334|precision=0.8495|recall=0.8141|f1=0.8314|auc=0.9338|aupr=0.9405|pos_acc=0.8141|neg_acc=0.8183\n",
      "-----------------------Fold =  1\n",
      "Train:\n",
      "tn = 16077, fp = 2744, fn = 3418, tp = 15454\n",
      "y_pred: 0 = 19495 | 1 = 18198\n",
      "y_true: 0 = 18821 | 1 = 18872\n",
      "acc=0.8365|precision=0.8492|recall=0.8189|f1=0.8338|auc=0.9348|aupr=0.9395|pos_acc=0.8189|neg_acc=0.8247\n",
      "Test:\n",
      "tn = 4039, fp = 698, fn = 829, tp = 3857\n",
      "y_pred: 0 = 4868 | 1 = 4555\n",
      "y_true: 0 = 4737 | 1 = 4686\n",
      "acc=0.8379|precision=0.8468|recall=0.8231|f1=0.8348|auc=0.9371|aupr=0.9410|pos_acc=0.8231|neg_acc=0.8297\n",
      "-----------------------Fold =  2\n",
      "Train:\n",
      "tn = 16112, fp = 2769, fn = 3413, tp = 15399\n",
      "y_pred: 0 = 19525 | 1 = 18168\n",
      "y_true: 0 = 18881 | 1 = 18812\n",
      "acc=0.8360|precision=0.8476|recall=0.8186|f1=0.8328|auc=0.9347|aupr=0.9395|pos_acc=0.8186|neg_acc=0.8252\n",
      "Test:\n",
      "tn = 4004, fp = 673, fn = 834, tp = 3912\n",
      "y_pred: 0 = 4838 | 1 = 4585\n",
      "y_true: 0 = 4677 | 1 = 4746\n",
      "acc=0.8401|precision=0.8532|recall=0.8243|f1=0.8385|auc=0.9350|aupr=0.9398|pos_acc=0.8243|neg_acc=0.8276\n",
      "-----------------------Fold =  3\n",
      "Train:\n",
      "tn = 16036, fp = 2758, fn = 3419, tp = 15480\n",
      "y_pred: 0 = 19455 | 1 = 18238\n",
      "y_true: 0 = 18794 | 1 = 18899\n",
      "acc=0.8361|precision=0.8488|recall=0.8191|f1=0.8337|auc=0.9356|aupr=0.9406|pos_acc=0.8191|neg_acc=0.8243\n",
      "Test:\n",
      "tn = 4080, fp = 684, fn = 828, tp = 3831\n",
      "y_pred: 0 = 4908 | 1 = 4515\n",
      "y_true: 0 = 4764 | 1 = 4659\n",
      "acc=0.8395|precision=0.8485|recall=0.8223|f1=0.8352|auc=0.9344|aupr=0.9368|pos_acc=0.8223|neg_acc=0.8313\n",
      "-----------------------Fold =  4\n",
      "Train:\n",
      "tn = 16105, fp = 2741, fn = 3375, tp = 15472\n",
      "y_pred: 0 = 19480 | 1 = 18213\n",
      "y_true: 0 = 18846 | 1 = 18847\n",
      "acc=0.8377|precision=0.8495|recall=0.8209|f1=0.8350|auc=0.9358|aupr=0.9399|pos_acc=0.8209|neg_acc=0.8267\n",
      "Test:\n",
      "tn = 4011, fp = 701, fn = 872, tp = 3839\n",
      "y_pred: 0 = 4883 | 1 = 4540\n",
      "y_true: 0 = 4712 | 1 = 4711\n",
      "acc=0.8331|precision=0.8456|recall=0.8149|f1=0.8300|auc=0.9349|aupr=0.9399|pos_acc=0.8149|neg_acc=0.8214\n"
     ]
    }
   ],
   "source": [
    "for isbalance in [True]:\n",
    "    \n",
    "    IPE, IG, dtp, gene_ids, peco_ids, gene_test_num, peco_test_num, samples = obtain_data(directory, \n",
    "                                                                                                 isbalance)\n",
    "    for task in ['Tp','Tg','Tpe']:\n",
    "        \n",
    "        print('========== isbalance = {} | task = {}'.format(isbalance, task))\n",
    "        \n",
    "        if task == 'Tp':\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tp_train_test_idx(samples)\n",
    "            \n",
    "        elif task == 'Tg':\n",
    "            item = 'gene_idx'\n",
    "            ids = gene_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tg_Tpe_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        elif task == 'Tpe':\n",
    "            item = 'peco_idx'\n",
    "            ids = peco_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tg_Tpe_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        ys_train, metrics_train, ys_test, metrics_test = run_clf(train_index_all, test_index_all, samples, 'GNB')\n",
    "        precision, recall, threshold = precision_recall_curve(ys_test[0], ys_test[2], pos_label=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "5it [00:00, 227.25it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# gene = 11177 | peco = 24\n",
      "# Test: gene = 2235 | peco = 4\n",
      "========== isbalance = True | task = Tp\n",
      "-------Fold  0\n",
      "# Pairs: Train = 37692 | Test = 9424\n",
      "-------Fold  1\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-------Fold  2\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-------Fold  3\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-------Fold  4\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-----------------------Fold =  0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train:\n",
      "tn = 18890, fp = 0, fn = 0, tp = 18802\n",
      "y_pred: 0 = 18890 | 1 = 18802\n",
      "y_true: 0 = 18890 | 1 = 18802\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 3806, fp = 862, fn = 922, tp = 3834\n",
      "y_pred: 0 = 4728 | 1 = 4696\n",
      "y_true: 0 = 4668 | 1 = 4756\n",
      "acc=0.8107|precision=0.8164|recall=0.8061|f1=0.8113|auc=0.8107|aupr=0.8602|pos_acc=0.8061|neg_acc=0.8050\n",
      "-----------------------Fold =  1\n",
      "Train:\n",
      "tn = 18821, fp = 0, fn = 0, tp = 18872\n",
      "y_pred: 0 = 18821 | 1 = 18872\n",
      "y_true: 0 = 18821 | 1 = 18872\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 3802, fp = 935, fn = 846, tp = 3840\n",
      "y_pred: 0 = 4648 | 1 = 4775\n",
      "y_true: 0 = 4737 | 1 = 4686\n",
      "acc=0.8110|precision=0.8042|recall=0.8195|f1=0.8118|auc=0.8110|aupr=0.8567|pos_acc=0.8195|neg_acc=0.8180\n",
      "-----------------------Fold =  2\n",
      "Train:\n",
      "tn = 18881, fp = 0, fn = 0, tp = 18812\n",
      "y_pred: 0 = 18881 | 1 = 18812\n",
      "y_true: 0 = 18881 | 1 = 18812\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 3797, fp = 880, fn = 907, tp = 3839\n",
      "y_pred: 0 = 4704 | 1 = 4719\n",
      "y_true: 0 = 4677 | 1 = 4746\n",
      "acc=0.8104|precision=0.8135|recall=0.8089|f1=0.8112|auc=0.8104|aupr=0.8593|pos_acc=0.8089|neg_acc=0.8072\n",
      "-----------------------Fold =  3\n",
      "Train:\n",
      "tn = 18794, fp = 0, fn = 0, tp = 18899\n",
      "y_pred: 0 = 18794 | 1 = 18899\n",
      "y_true: 0 = 18794 | 1 = 18899\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 3831, fp = 933, fn = 899, tp = 3760\n",
      "y_pred: 0 = 4730 | 1 = 4693\n",
      "y_true: 0 = 4764 | 1 = 4659\n",
      "acc=0.8056|precision=0.8012|recall=0.8070|f1=0.8041|auc=0.8056|aupr=0.8518|pos_acc=0.8070|neg_acc=0.8099\n",
      "-----------------------Fold =  4\n",
      "Train:\n",
      "tn = 18846, fp = 0, fn = 0, tp = 18847\n",
      "y_pred: 0 = 18846 | 1 = 18847\n",
      "y_true: 0 = 18846 | 1 = 18847\n",
      "acc=1.0000|precision=1.0000|recall=1.0000|f1=1.0000|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "Test:\n",
      "tn = 3837, fp = 875, fn = 880, tp = 3831\n",
      "y_pred: 0 = 4717 | 1 = 4706\n",
      "y_true: 0 = 4712 | 1 = 4711\n",
      "acc=0.8138|precision=0.8141|recall=0.8132|f1=0.8136|auc=0.8138|aupr=0.8603|pos_acc=0.8132|neg_acc=0.8134\n"
     ]
    }
   ],
   "source": [
    "for isbalance in [True]:\n",
    "    \n",
    "    IPE, IG, dtp, gene_ids, peco_ids, gene_test_num, peco_test_num, samples = obtain_data(directory, \n",
    "                                                                                                 isbalance)\n",
    "    for task in ['Tp','Tg','Tpe']:\n",
    "        \n",
    "        print('========== isbalance = {} | task = {}'.format(isbalance, task))\n",
    "        \n",
    "        if task == 'Tp':\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tp_train_test_idx(samples)\n",
    "            \n",
    "        elif task == 'Tg':\n",
    "            item = 'gene_idx'\n",
    "            ids = gene_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tg_Tpe_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        elif task == 'Tpe':\n",
    "            item = 'peco_idx'\n",
    "            ids = peco_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tg_Tpe_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        ys_train, metrics_train, ys_test, metrics_test = run_clf(train_index_all, test_index_all, samples, 'DT')\n",
    "        precision, recall, threshold = precision_recall_curve(ys_test[0], ys_test[2], pos_label=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "5it [00:00, 294.00it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# gene = 11177 | peco = 24\n",
      "# Test: gene = 2235 | peco = 4\n",
      "========== isbalance = True | task = Tp\n",
      "-------Fold  0\n",
      "# Pairs: Train = 37692 | Test = 9424\n",
      "-------Fold  1\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-------Fold  2\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-------Fold  3\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-------Fold  4\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-----------------------Fold =  0\n",
      "Train:\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tn = 18814, fp = 76, fn = 10490, tp = 8312\n",
      "y_pred: 0 = 29304 | 1 = 8388\n",
      "y_true: 0 = 18890 | 1 = 18802\n",
      "acc=0.7197|precision=0.9909|recall=0.4421|f1=0.6114|auc=0.8491|aupr=0.9039|pos_acc=0.4421|neg_acc=0.6420\n",
      "Test:\n",
      "tn = 4657, fp = 11, fn = 2643, tp = 2113\n",
      "y_pred: 0 = 7300 | 1 = 2124\n",
      "y_true: 0 = 4668 | 1 = 4756\n",
      "acc=0.7184|precision=0.9948|recall=0.4443|f1=0.6142|auc=0.8525|aupr=0.9083|pos_acc=0.4443|neg_acc=0.6379\n",
      "-----------------------Fold =  1\n",
      "Train:\n",
      "tn = 16867, fp = 1954, fn = 3538, tp = 15334\n",
      "y_pred: 0 = 20405 | 1 = 17288\n",
      "y_true: 0 = 18821 | 1 = 18872\n",
      "acc=0.8543|precision=0.8870|recall=0.8125|f1=0.8481|auc=0.9143|aupr=0.9322|pos_acc=0.8125|neg_acc=0.8266\n",
      "Test:\n",
      "tn = 4266, fp = 471, fn = 894, tp = 3792\n",
      "y_pred: 0 = 5160 | 1 = 4263\n",
      "y_true: 0 = 4737 | 1 = 4686\n",
      "acc=0.8551|precision=0.8895|recall=0.8092|f1=0.8475|auc=0.9163|aupr=0.9340|pos_acc=0.8092|neg_acc=0.8267\n",
      "-----------------------Fold =  2\n",
      "Train:\n",
      "tn = 14513, fp = 4368, fn = 1187, tp = 17625\n",
      "y_pred: 0 = 15700 | 1 = 21993\n",
      "y_true: 0 = 18881 | 1 = 18812\n",
      "acc=0.8526|precision=0.8014|recall=0.9369|f1=0.8639|auc=0.9171|aupr=0.9143|pos_acc=0.9369|neg_acc=0.9244\n",
      "Test:\n",
      "tn = 3610, fp = 1067, fn = 298, tp = 4448\n",
      "y_pred: 0 = 3908 | 1 = 5515\n",
      "y_true: 0 = 4677 | 1 = 4746\n",
      "acc=0.8551|precision=0.8065|recall=0.9372|f1=0.8670|auc=0.9197|aupr=0.9188|pos_acc=0.9372|neg_acc=0.9237\n",
      "-----------------------Fold =  3\n",
      "Train:\n",
      "tn = 15340, fp = 3454, fn = 2199, tp = 16700\n",
      "y_pred: 0 = 17539 | 1 = 20154\n",
      "y_true: 0 = 18794 | 1 = 18899\n",
      "acc=0.8500|precision=0.8286|recall=0.8836|f1=0.8552|auc=0.9274|aupr=0.9323|pos_acc=0.8836|neg_acc=0.8746\n",
      "Test:\n",
      "tn = 3876, fp = 888, fn = 512, tp = 4147\n",
      "y_pred: 0 = 4388 | 1 = 5035\n",
      "y_true: 0 = 4764 | 1 = 4659\n",
      "acc=0.8514|precision=0.8236|recall=0.8901|f1=0.8556|auc=0.9273|aupr=0.9303|pos_acc=0.8901|neg_acc=0.8833\n",
      "-----------------------Fold =  4\n",
      "Train:\n",
      "tn = 17302, fp = 1544, fn = 4115, tp = 14732\n",
      "y_pred: 0 = 21417 | 1 = 16276\n",
      "y_true: 0 = 18846 | 1 = 18847\n",
      "acc=0.8499|precision=0.9051|recall=0.7817|f1=0.8389|auc=0.9279|aupr=0.9358|pos_acc=0.7817|neg_acc=0.8079\n",
      "Test:\n",
      "tn = 4313, fp = 399, fn = 1025, tp = 3686\n",
      "y_pred: 0 = 5338 | 1 = 4085\n",
      "y_true: 0 = 4712 | 1 = 4711\n",
      "acc=0.8489|precision=0.9023|recall=0.7824|f1=0.8381|auc=0.9262|aupr=0.9349|pos_acc=0.7824|neg_acc=0.8080\n"
     ]
    }
   ],
   "source": [
    "for isbalance in [True]:\n",
    "    \n",
    "    IPE, IG, dtp, gene_ids, peco_ids, gene_test_num, peco_test_num, samples = obtain_data(directory, \n",
    "                                                                                                 isbalance)\n",
    "    for task in ['Tp','Tg','Tpe']:\n",
    "        \n",
    "        print('========== isbalance = {} | task = {}'.format(isbalance, task))\n",
    "        \n",
    "        if task == 'Tp':\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tp_train_test_idx(samples)\n",
    "            \n",
    "        elif task == 'Tg':\n",
    "            item = 'gene_idx'\n",
    "            ids = gene_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tg_Tpe_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        elif task == 'Tpe':\n",
    "            item = 'peco_idx'\n",
    "            ids = peco_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tg_Tpe_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        ys_train, metrics_train, ys_test, metrics_test = run_clf(train_index_all, test_index_all, samples, 'SGD')\n",
    "        precision, recall, threshold = precision_recall_curve(ys_test[0], ys_test[2], pos_label=1)\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23558\n",
      "1528\n",
      "10396\n",
      "86\n",
      "121\n",
      "1179\n",
      "126\n",
      "5\n",
      "14\n",
      "45\n",
      "4992\n",
      "7\n",
      "67\n",
      "312\n",
      "853\n",
      "148\n",
      "29\n",
      "831\n",
      "45\n",
      "476\n",
      "2239\n",
      "1\n",
      "51\n",
      "1\n",
      "6\n",
      "# gene = 11177 | peco = 24\n",
      "# Test: gene = 2235 | peco = 4\n",
      "========== isbalance = True | task = Tpe\n",
      "-------Fold  0\n",
      "# peco_idx: Train = 20 | Test = 4\n",
      "# Pairs: Train = 45066 | Test = 2050\n",
      "-------Fold  1\n",
      "# peco_idx: Train = 20 | Test = 4\n",
      "# Pairs: Train = 43726 | Test = 3390\n",
      "-------Fold  2\n",
      "# peco_idx: Train = 20 | Test = 4\n",
      "# Pairs: Train = 42256 | Test = 4860\n",
      "-------Fold  3\n",
      "# peco_idx: Train = 20 | Test = 4\n",
      "# Pairs: Train = 44564 | Test = 2552\n",
      "-------Fold  4\n",
      "# peco_idx: Train = 16 | Test = 8\n",
      "# Pairs: Train = 12852 | Test = 34264\n",
      "-----------------------Fold =  0\n",
      "Train:\n",
      "tn = 11772, fp = 10497, fn = 10988, tp = 11809\n",
      "y_pred: 0 = 22760 | 1 = 22306\n",
      "y_true: 0 = 22269 | 1 = 22797\n",
      "acc=0.5233|precision=0.5294|recall=0.5180|f1=0.5236|auc=0.5358|aupr=0.5405|pos_acc=0.5180|neg_acc=0.5172\n",
      "Test:\n",
      "tn = 628, fp = 661, fn = 396, tp = 365\n",
      "y_pred: 0 = 1024 | 1 = 1026\n",
      "y_true: 0 = 1289 | 1 = 761\n",
      "acc=0.4844|precision=0.3558|recall=0.4796|f1=0.4085|auc=0.4903|aupr=0.3737|pos_acc=0.4796|neg_acc=0.6133\n",
      "-----------------------Fold =  1\n",
      "Train:\n",
      "tn = 16326, fp = 6053, fn = 14479, tp = 6868\n",
      "y_pred: 0 = 30805 | 1 = 12921\n",
      "y_true: 0 = 22379 | 1 = 21347\n",
      "acc=0.5304|precision=0.5315|recall=0.3217|f1=0.4008|auc=0.5394|aupr=0.5244|pos_acc=0.3217|neg_acc=0.5300\n",
      "Test:\n",
      "tn = 749, fp = 430, fn = 1491, tp = 720\n",
      "y_pred: 0 = 2240 | 1 = 1150\n",
      "y_true: 0 = 1179 | 1 = 2211\n",
      "acc=0.4333|precision=0.6261|recall=0.3256|f1=0.4284|auc=0.4565|aupr=0.6210|pos_acc=0.3256|neg_acc=0.3344\n",
      "-----------------------Fold =  2\n",
      "Train:\n",
      "tn = 8128, fp = 12359, fn = 7616, tp = 14153\n",
      "y_pred: 0 = 15744 | 1 = 26512\n",
      "y_true: 0 = 20487 | 1 = 21769\n",
      "acc=0.5273|precision=0.5338|recall=0.6501|f1=0.5863|auc=0.5409|aupr=0.5541|pos_acc=0.6501|neg_acc=0.5163\n",
      "Test:\n",
      "tn = 934, fp = 2137, fn = 684, tp = 1105\n",
      "y_pred: 0 = 1618 | 1 = 3242\n",
      "y_true: 0 = 3071 | 1 = 1789\n",
      "acc=0.4195|precision=0.3408|recall=0.6177|f1=0.4393|auc=0.4570|aupr=0.3455|pos_acc=0.6177|neg_acc=0.5773\n",
      "-----------------------Fold =  3\n",
      "Train:\n",
      "tn = 13930, fp = 8334, fn = 12551, tp = 9749\n",
      "y_pred: 0 = 26481 | 1 = 18083\n",
      "y_true: 0 = 22264 | 1 = 22300\n",
      "acc=0.5313|precision=0.5391|recall=0.4372|f1=0.4828|auc=0.5402|aupr=0.5389|pos_acc=0.4372|neg_acc=0.5260\n",
      "Test:\n",
      "tn = 645, fp = 649, fn = 859, tp = 399\n",
      "y_pred: 0 = 1504 | 1 = 1048\n",
      "y_true: 0 = 1294 | 1 = 1258\n",
      "acc=0.4091|precision=0.3807|recall=0.3172|f1=0.3461|auc=0.4012|aupr=0.4181|pos_acc=0.3172|neg_acc=0.4289\n",
      "-----------------------Fold =  4\n",
      "Train:\n",
      "tn = 5465, fp = 1368, fn = 4527, tp = 1492\n",
      "y_pred: 0 = 9992 | 1 = 2860\n",
      "y_true: 0 = 6833 | 1 = 6019\n",
      "acc=0.5413|precision=0.5217|recall=0.2479|f1=0.3361|auc=0.5567|aupr=0.5158|pos_acc=0.2479|neg_acc=0.5469\n",
      "Test:\n",
      "tn = 13057, fp = 3668, fn = 13360, tp = 4179\n",
      "y_pred: 0 = 26417 | 1 = 7847\n",
      "y_true: 0 = 16725 | 1 = 17539\n",
      "acc=0.5030|precision=0.5326|recall=0.2383|f1=0.3292|auc=0.5093|aupr=0.5227|pos_acc=0.2383|neg_acc=0.4943\n"
     ]
    }
   ],
   "source": [
    "for isbalance in [True]:\n",
    "    \n",
    "    IPE, IG, dtp, gene_ids, peco_ids, gene_test_num, peco_test_num, samples = obtain_data(directory, \n",
    "                                                                                                 isbalance)\n",
    "    for task in ['Tp','Tg','Tpe']:\n",
    "        \n",
    "        print('========== isbalance = {} | task = {}'.format(isbalance, task))\n",
    "        \n",
    "        if task == 'Tp':\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tp_train_test_idx(samples)\n",
    "            \n",
    "        elif task == 'Tg':\n",
    "            item = 'gene_idx'\n",
    "            ids = gene_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tg_Tpe_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        elif task == 'Tpe':\n",
    "            item = 'peco_idx'\n",
    "            ids = peco_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tg_Tpe_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        ys_train, metrics_train, ys_test, metrics_test = run_clf(train_index_all, test_index_all, samples, 'LR')\n",
    "        precision, recall, threshold = precision_recall_curve(ys_test[0], ys_test[2], pos_label=1)\n",
    "        data1 = pd.DataFrame(zip(precision, recall), columns=['precision','recall']).to_csv(r'D:\\小麦\\MDA-GCNFTG-main\\MDA-GCNFTG-main\\评价指标统计图\\prc\\LR.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23558\n",
      "1528\n",
      "10396\n",
      "86\n",
      "121\n",
      "1179\n",
      "126\n",
      "5\n",
      "14\n",
      "45\n",
      "4992\n",
      "7\n",
      "67\n",
      "312\n",
      "853\n",
      "148\n",
      "29\n",
      "831\n",
      "45\n",
      "476\n",
      "2239\n",
      "1\n",
      "51\n",
      "1\n",
      "6\n",
      "# gene = 11177 | peco = 24\n",
      "# Test: gene = 2235 | peco = 4\n",
      "========== isbalance = True | task = Tpe\n",
      "-------Fold  0\n",
      "# peco_idx: Train = 20 | Test = 4\n",
      "# Pairs: Train = 45322 | Test = 1794\n",
      "-------Fold  1\n",
      "# peco_idx: Train = 20 | Test = 4\n",
      "# Pairs: Train = 44404 | Test = 2712\n",
      "-------Fold  2\n",
      "# peco_idx: Train = 20 | Test = 4\n",
      "# Pairs: Train = 15386 | Test = 31730\n",
      "-------Fold  3\n",
      "# peco_idx: Train = 20 | Test = 4\n",
      "# Pairs: Train = 45144 | Test = 1972\n",
      "-------Fold  4\n",
      "# peco_idx: Train = 16 | Test = 8\n",
      "# Pairs: Train = 38208 | Test = 8908\n",
      "-----------------------Fold =  0\n",
      "Train:\n",
      "tn = 18237, fp = 4188, fn = 14837, tp = 8060\n",
      "y_pred: 0 = 33074 | 1 = 12248\n",
      "y_true: 0 = 22425 | 1 = 22897\n",
      "acc=0.5802|precision=0.6581|recall=0.3520|f1=0.4587|auc=0.6341|aupr=0.6495|pos_acc=0.3520|neg_acc=0.5514\n",
      "Test:\n",
      "tn = 770, fp = 363, fn = 453, tp = 208\n",
      "y_pred: 0 = 1223 | 1 = 571\n",
      "y_true: 0 = 1133 | 1 = 661\n",
      "acc=0.5452|precision=0.3643|recall=0.3147|f1=0.3377|auc=0.5126|aupr=0.3767|pos_acc=0.3147|neg_acc=0.6296\n",
      "-----------------------Fold =  1\n",
      "Train:\n",
      "tn = 12465, fp = 9717, fn = 8823, tp = 13399\n",
      "y_pred: 0 = 21288 | 1 = 23116\n",
      "y_true: 0 = 22182 | 1 = 22222\n",
      "acc=0.5825|precision=0.5796|recall=0.6030|f1=0.5911|auc=0.6285|aupr=0.6382|pos_acc=0.6030|neg_acc=0.5855\n",
      "Test:\n",
      "tn = 594, fp = 782, fn = 626, tp = 710\n",
      "y_pred: 0 = 1220 | 1 = 1492\n",
      "y_true: 0 = 1376 | 1 = 1336\n",
      "acc=0.4808|precision=0.4759|recall=0.5314|f1=0.5021|auc=0.4784|aupr=0.4967|pos_acc=0.5314|neg_acc=0.4869\n",
      "-----------------------Fold =  2\n",
      "Train:\n",
      "tn = 5913, fp = 2556, fn = 3052, tp = 3865\n",
      "y_pred: 0 = 8965 | 1 = 6421\n",
      "y_true: 0 = 8469 | 1 = 6917\n",
      "acc=0.6355|precision=0.6019|recall=0.5588|f1=0.5795|auc=0.6895|aupr=0.6532|pos_acc=0.5588|neg_acc=0.6596\n",
      "Test:\n",
      "tn = 8067, fp = 7022, fn = 8868, tp = 7773\n",
      "y_pred: 0 = 16935 | 1 = 14795\n",
      "y_true: 0 = 15089 | 1 = 16641\n",
      "acc=0.4992|precision=0.5254|recall=0.4671|f1=0.4945|auc=0.4988|aupr=0.5303|pos_acc=0.4671|neg_acc=0.4764\n",
      "-----------------------Fold =  3\n",
      "Train:\n",
      "tn = 14262, fp = 8064, fn = 10178, tp = 12640\n",
      "y_pred: 0 = 24440 | 1 = 20704\n",
      "y_true: 0 = 22326 | 1 = 22818\n",
      "acc=0.5959|precision=0.6105|recall=0.5539|f1=0.5809|auc=0.6382|aupr=0.6510|pos_acc=0.5539|neg_acc=0.5836\n",
      "Test:\n",
      "tn = 610, fp = 622, fn = 391, tp = 349\n",
      "y_pred: 0 = 1001 | 1 = 971\n",
      "y_true: 0 = 1232 | 1 = 740\n",
      "acc=0.4863|precision=0.3594|recall=0.4716|f1=0.4079|auc=0.4907|aupr=0.4011|pos_acc=0.4716|neg_acc=0.6094\n",
      "-----------------------Fold =  4\n",
      "Train:\n",
      "tn = 13328, fp = 5502, fn = 9644, tp = 9734\n",
      "y_pred: 0 = 22972 | 1 = 15236\n",
      "y_true: 0 = 18830 | 1 = 19378\n",
      "acc=0.6036|precision=0.6389|recall=0.5023|f1=0.5624|auc=0.6563|aupr=0.6794|pos_acc=0.5023|neg_acc=0.5802\n",
      "Test:\n",
      "tn = 2395, fp = 2333, fn = 2266, tp = 1914\n",
      "y_pred: 0 = 4661 | 1 = 4247\n",
      "y_true: 0 = 4728 | 1 = 4180\n",
      "acc=0.4837|precision=0.4507|recall=0.4579|f1=0.4543|auc=0.4699|aupr=0.4484|pos_acc=0.4579|neg_acc=0.5138\n"
     ]
    }
   ],
   "source": [
    "for isbalance in [True]:\n",
    "    \n",
    "    IPE, IG, dtp, gene_ids, peco_ids, gene_test_num, peco_test_num, samples = obtain_data(directory, \n",
    "                                                                                                 isbalance)\n",
    "    for task in ['Tp','Tg','Tpe']:\n",
    "        \n",
    "        print('========== isbalance = {} | task = {}'.format(isbalance, task))\n",
    "        \n",
    "        if task == 'Tp':\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tp_train_test_idx(samples)\n",
    "            \n",
    "        elif task == 'Tg':\n",
    "            item = 'gene_idx'\n",
    "            ids = gene_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tg_Tpe_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        elif task == 'Tpe':\n",
    "            item = 'peco_idx'\n",
    "            ids = peco_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tg_Tpe_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        ys_train, metrics_train, ys_test, metrics_test = run_clf(train_index_all, test_index_all, samples, 'MLP')\n",
    "        precision, recall, threshold = precision_recall_curve(ys_test[0], ys_test[2], pos_label=1)\n",
    "        data1 = pd.DataFrame(zip(precision, recall), columns=['precision','recall']).to_csv(r'D:\\小麦\\MDA-GCNFTG-main\\MDA-GCNFTG-main\\评价指标统计图\\prc\\MLP.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "5it [00:00, 147.06it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# gene = 11177 | peco = 24\n",
      "# Test: gene = 2235 | peco = 4\n",
      "========== isbalance = True | task = Tp\n",
      "-------Fold  0\n",
      "# Pairs: Train = 37692 | Test = 9424\n",
      "-------Fold  1\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-------Fold  2\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-------Fold  3\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-------Fold  4\n",
      "# Pairs: Train = 37693 | Test = 9423\n",
      "-----------------------Fold =  0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train:\n",
      "tn = 16259, fp = 2631, fn = 1917, tp = 16885\n",
      "y_pred: 0 = 18176 | 1 = 19516\n",
      "y_true: 0 = 18890 | 1 = 18802\n",
      "acc=0.8793|precision=0.8652|recall=0.8980|f1=0.8813|auc=0.9567|aupr=0.9584|pos_acc=0.8980|neg_acc=0.8945\n",
      "Test:\n",
      "tn = 3944, fp = 724, fn = 565, tp = 4191\n",
      "y_pred: 0 = 4509 | 1 = 4915\n",
      "y_true: 0 = 4668 | 1 = 4756\n",
      "acc=0.8632|precision=0.8527|recall=0.8812|f1=0.8667|auc=0.9432|aupr=0.9452|pos_acc=0.8812|neg_acc=0.8747\n",
      "-----------------------Fold =  1\n",
      "Train:\n",
      "tn = 16297, fp = 2524, fn = 2004, tp = 16868\n",
      "y_pred: 0 = 18301 | 1 = 19392\n",
      "y_true: 0 = 18821 | 1 = 18872\n",
      "acc=0.8799|precision=0.8698|recall=0.8938|f1=0.8817|auc=0.9567|aupr=0.9585|pos_acc=0.8938|neg_acc=0.8905\n",
      "Test:\n",
      "tn = 4040, fp = 697, fn = 572, tp = 4114\n",
      "y_pred: 0 = 4612 | 1 = 4811\n",
      "y_true: 0 = 4737 | 1 = 4686\n",
      "acc=0.8653|precision=0.8551|recall=0.8779|f1=0.8664|auc=0.9450|aupr=0.9443|pos_acc=0.8779|neg_acc=0.8760\n",
      "-----------------------Fold =  2\n",
      "Train:\n",
      "tn = 16330, fp = 2551, fn = 1955, tp = 16857\n",
      "y_pred: 0 = 18285 | 1 = 19408\n",
      "y_true: 0 = 18881 | 1 = 18812\n",
      "acc=0.8805|precision=0.8686|recall=0.8961|f1=0.8821|auc=0.9565|aupr=0.9583|pos_acc=0.8961|neg_acc=0.8931\n",
      "Test:\n",
      "tn = 3965, fp = 712, fn = 580, tp = 4166\n",
      "y_pred: 0 = 4545 | 1 = 4878\n",
      "y_true: 0 = 4677 | 1 = 4746\n",
      "acc=0.8629|precision=0.8540|recall=0.8778|f1=0.8658|auc=0.9440|aupr=0.9440|pos_acc=0.8778|neg_acc=0.8724\n",
      "-----------------------Fold =  3\n",
      "Train:\n",
      "tn = 16209, fp = 2585, fn = 1927, tp = 16972\n",
      "y_pred: 0 = 18136 | 1 = 19557\n",
      "y_true: 0 = 18794 | 1 = 18899\n",
      "acc=0.8803|precision=0.8678|recall=0.8980|f1=0.8827|auc=0.9568|aupr=0.9591|pos_acc=0.8980|neg_acc=0.8937\n",
      "Test:\n",
      "tn = 4011, fp = 753, fn = 579, tp = 4080\n",
      "y_pred: 0 = 4590 | 1 = 4833\n",
      "y_true: 0 = 4764 | 1 = 4659\n",
      "acc=0.8586|precision=0.8442|recall=0.8757|f1=0.8597|auc=0.9418|aupr=0.9400|pos_acc=0.8757|neg_acc=0.8739\n",
      "-----------------------Fold =  4\n",
      "Train:\n",
      "tn = 16277, fp = 2569, fn = 1930, tp = 16917\n",
      "y_pred: 0 = 18207 | 1 = 19486\n",
      "y_true: 0 = 18846 | 1 = 18847\n",
      "acc=0.8806|precision=0.8682|recall=0.8976|f1=0.8826|auc=0.9571|aupr=0.9590|pos_acc=0.8976|neg_acc=0.8940\n",
      "Test:\n",
      "tn = 3961, fp = 751, fn = 571, tp = 4140\n",
      "y_pred: 0 = 4532 | 1 = 4891\n",
      "y_true: 0 = 4712 | 1 = 4711\n",
      "acc=0.8597|precision=0.8465|recall=0.8788|f1=0.8623|auc=0.9431|aupr=0.9431|pos_acc=0.8788|neg_acc=0.8740\n",
      "========== isbalance = True | task = Tg\n",
      "-------Fold  0\n",
      "# gene_idx: Train = 8942 | Test = 2235\n",
      "# Pairs: Train = 37776 | Test = 9340\n",
      "-------Fold  1\n",
      "# gene_idx: Train = 8942 | Test = 2235\n",
      "# Pairs: Train = 37780 | Test = 9336\n",
      "-------Fold  2\n",
      "# gene_idx: Train = 8942 | Test = 2235\n",
      "# Pairs: Train = 37540 | Test = 9576\n",
      "-------Fold  3\n",
      "# gene_idx: Train = 8942 | Test = 2235\n",
      "# Pairs: Train = 37733 | Test = 9383\n",
      "-------Fold  4\n",
      "# gene_idx: Train = 8940 | Test = 2237\n",
      "# Pairs: Train = 37635 | Test = 9481\n",
      "-----------------------Fold =  0\n",
      "Train:\n",
      "tn = 16392, fp = 2509, fn = 1937, tp = 16938\n",
      "y_pred: 0 = 18329 | 1 = 19447\n",
      "y_true: 0 = 18901 | 1 = 18875\n",
      "acc=0.8823|precision=0.8710|recall=0.8974|f1=0.8840|auc=0.9576|aupr=0.9594|pos_acc=0.8974|neg_acc=0.8943\n",
      "Test:\n",
      "tn = 3911, fp = 746, fn = 573, tp = 4110\n",
      "y_pred: 0 = 4484 | 1 = 4856\n",
      "y_true: 0 = 4657 | 1 = 4683\n",
      "acc=0.8588|precision=0.8464|recall=0.8776|f1=0.8617|auc=0.9417|aupr=0.9435|pos_acc=0.8776|neg_acc=0.8722\n",
      "-----------------------Fold =  1\n",
      "Train:\n",
      "tn = 16288, fp = 2571, fn = 1989, tp = 16932\n",
      "y_pred: 0 = 18277 | 1 = 19503\n",
      "y_true: 0 = 18859 | 1 = 18921\n",
      "acc=0.8793|precision=0.8682|recall=0.8949|f1=0.8813|auc=0.9564|aupr=0.9585|pos_acc=0.8949|neg_acc=0.8912\n",
      "Test:\n",
      "tn = 3956, fp = 743, fn = 540, tp = 4097\n",
      "y_pred: 0 = 4496 | 1 = 4840\n",
      "y_true: 0 = 4699 | 1 = 4637\n",
      "acc=0.8626|precision=0.8465|recall=0.8835|f1=0.8646|auc=0.9442|aupr=0.9421|pos_acc=0.8835|neg_acc=0.8799\n",
      "-----------------------Fold =  2\n",
      "Train:\n",
      "tn = 16266, fp = 2519, fn = 1963, tp = 16792\n",
      "y_pred: 0 = 18229 | 1 = 19311\n",
      "y_true: 0 = 18785 | 1 = 18755\n",
      "acc=0.8806|precision=0.8696|recall=0.8953|f1=0.8823|auc=0.9573|aupr=0.9590|pos_acc=0.8953|neg_acc=0.8923\n",
      "Test:\n",
      "tn = 4011, fp = 762, fn = 634, tp = 4169\n",
      "y_pred: 0 = 4645 | 1 = 4931\n",
      "y_true: 0 = 4773 | 1 = 4803\n",
      "acc=0.8542|precision=0.8455|recall=0.8680|f1=0.8566|auc=0.9385|aupr=0.9380|pos_acc=0.8680|neg_acc=0.8635\n",
      "-----------------------Fold =  3\n",
      "Train:\n",
      "tn = 16248, fp = 2645, fn = 1950, tp = 16890\n",
      "y_pred: 0 = 18198 | 1 = 19535\n",
      "y_true: 0 = 18893 | 1 = 18840\n",
      "acc=0.8782|precision=0.8646|recall=0.8965|f1=0.8803|auc=0.9557|aupr=0.9574|pos_acc=0.8965|neg_acc=0.8928\n",
      "Test:\n",
      "tn = 3962, fp = 703, fn = 543, tp = 4175\n",
      "y_pred: 0 = 4505 | 1 = 4878\n",
      "y_true: 0 = 4665 | 1 = 4718\n",
      "acc=0.8672|precision=0.8559|recall=0.8849|f1=0.8702|auc=0.9480|aupr=0.9487|pos_acc=0.8849|neg_acc=0.8795\n",
      "-----------------------Fold =  4\n",
      "Train:\n",
      "tn = 16170, fp = 2624, fn = 1928, tp = 16913\n",
      "y_pred: 0 = 18098 | 1 = 19537\n",
      "y_true: 0 = 18794 | 1 = 18841\n",
      "acc=0.8790|precision=0.8657|recall=0.8977|f1=0.8814|auc=0.9560|aupr=0.9578|pos_acc=0.8977|neg_acc=0.8935\n",
      "Test:\n",
      "tn = 4035, fp = 729, fn = 579, tp = 4138\n",
      "y_pred: 0 = 4614 | 1 = 4867\n",
      "y_true: 0 = 4764 | 1 = 4717\n",
      "acc=0.8620|precision=0.8502|recall=0.8773|f1=0.8635|auc=0.9454|aupr=0.9451|pos_acc=0.8773|neg_acc=0.8745\n"
     ]
    }
   ],
   "source": [
    "directory = 'D:\\小麦\\MDA-GCNFTG-main\\MDA-GCNFTG-main\\data'\n",
    "for isbalance in [True]:\n",
    "    \n",
    "    IPE, IG, dtp, gene_ids, peco_ids, gene_test_num, peco_test_num, samples = obtain_data(directory, \n",
    "                                                                                                 isbalance)\n",
    "    for task in ['Tp','Tg','Tpe']:\n",
    "        \n",
    "        print('========== isbalance = {} | task = {}'.format(isbalance, task))\n",
    "        \n",
    "        if task == 'Tp':\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tp_train_test_idx(samples)\n",
    "            \n",
    "        elif task == 'Tg':\n",
    "            item = 'gene_idx'\n",
    "            ids = gene_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tg_Tpe_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        elif task == 'Tpe':\n",
    "            item = 'peco_idx'\n",
    "            ids = peco_ids\n",
    "            train_index_all, test_index_all, train_id_all, test_id_all = generate_task_Tg_Tpe_train_test_idx(item, ids, dtp)\n",
    "\n",
    "        ys_train, metrics_train, ys_test, metrics_test = run_clf(train_index_all, test_index_all, samples, 'GBDT')\n",
    "        precision, recall, threshold = precision_recall_curve(ys_test[0], ys_test[2], pos_label=1)\n",
    "        data1 = pd.DataFrame(zip(precision, recall), columns=['precision','recall']).to_csv(r'D:\\小麦\\MDA-GCNFTG-main\\MDA-GCNFTG-main\\评价指标统计图\\prc\\GBDT.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
